package com.hk.controller;

import com.hk.client.OSSAliyunFileStorageService;
import com.hk.config.OssAliyunAutoConfig;
import com.hk.context.BaseContext;
import com.hk.core.R;
import com.hk.core.Result;
import com.hk.properties.AliOssConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/common/upload")
@Slf4j
public class UploadFile {
    @Autowired
    OSSAliyunFileStorageService oss;
    @Autowired
    RedisTemplate<String,String> redisTemplate;

    /**
     * 上传文件
     */
    @PostMapping
    public R upload(MultipartFile file) throws IOException {
        String url = oss.store(file.getOriginalFilename(), file.getInputStream());
        Integer userId = BaseContext.getCurrent().getUserId();
        redisTemplate.opsForValue().set(userId.toString(),url,30, TimeUnit.MINUTES);
        log.info("上传文件成功，文件地址{}",url);
        return R.ok(url);
    }
}
